import {
  PayWayEnum, findMapKey
} from '@/utils/type'

export class Pay {
  static modules = new Map()
  static inject(name, module) {
    this.modules.set(name, module)
  }
  constructor() {
    //动态注入支付方式
    for (const [name, module] of Pay.modules.entries()) {
      module.init(name, this)
    }
  }

  //调用支付
  async payment(payWay, options) {
    console.log('pay.payment', payWay, options);
    
    try {
      var way = findMapKey(PayWayEnum, payWay)
      const module = Pay.modules.get(way)
      if (!module) {
        throw new Error(`can not find pay way ${way}`)
      }
      return await module.run(options)
    } catch (error) {
      return Promise.reject(error)
    }
  }
}
